var DY = {};

DY.isNum = function(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
};
DY.checkNumber = function(ipt){
    var val = parseInt(ipt.val());
    if(!DY.isNum(val)){
        ipt.val(1);
    }
    if(val < 1){
        ipt.val(1);
    }
    if(val > 10){
        ipt.val(10);
    }
};
DY.hash = function(key, value, remove){
    var hash = window.location.hash.replace('#', '');
    var hashMap = {};
    var hashArray = hash.split('&');
    for(var i = 0;i<hashArray.length;i++){
        hashMap[hashArray[i].split('=')[0]] = hashArray[i].split('=')[1];
    }
    
    
    if(key && remove){
        hashMap[key] = null;
        hashArray = [];
        for(var h in hashMap){
            if(hashMap[h]){hashArray.push(h + '=' + hashMap[h]);}
        }
        window.location.hash = '#' + hashArray.join('&');
    }else if(key && value){
        hashMap[key] = value;
        hashArray = [];
        for(var h in hashMap){
            if(hashMap[h]){hashArray.push(h + '=' + hashMap[h]);}
        }
        window.location.hash = '#' + hashArray.join('&');
    }else if(key){
        if(hashMap[key]){
            return hashMap[key];
        }else{
            return null;
        }
    }else{
        return window.location.hash;
    }
};
DY.openIframeWindow = function(url, width, height){
    var ifmDiv = $('<div class="popupwin"></div>').appendTo(document.body).css({
        width: width,
        height: height,
        marginLeft: -width/2
    });
    var ifm = $('<iframe frameborder=no src="' + url + '"></iframe>').appendTo(ifmDiv).css({
        width: width,
        height: height
    });
};


$.fn.dySlideshow = function(){
    var timer = null;
    var index = 0;
    var maxIndex = 0;
    var btnContainer = $('<ul class="btn"></ul>').appendTo(this);
    var imgContainer = this.children('ul.img').scrollTop(0);
    var imgs = this.find('.img li').each(function(i){
        btnContainer.append('<li idx="' + i + '"><a href="javascript:void(0);">' + (i+1) + '</a></li>');
    });
    maxIndex = imgs.length;
    var imgHeight = imgs.height();
    imgContainer.height(imgHeight);
    
    var btns = this.find('.btn li').click(function(e){
        index = this.getAttribute('idx');
        imgContainer.stop().animate({
            scrollTop: index * imgHeight
        }, 500);
    });
    
    var start = function(){
        timer = setInterval(function(){
            index = index >= maxIndex - 1 ? 0 : index + 1;
            imgContainer.stop().animate({
                scrollTop: index * imgHeight
            }, 500);
        }, 3000);
    };
    var stop = function(){
        clearInterval(timer);
    };
    start();
    this.hover(stop, start);
};
DY.loading = function(isLoading){
    if(!DY._loadingEl){
        DY._loadingEl = $('<div class="dy-loading"><img src="image/loading.gif" /></div>').appendTo(document.body);
    }
    
    if(isLoading === false){
        DY._loadingEl.hide();
    }else{
        DY._loadingEl.show();
    }
};
DY.msg = function(msg){
    if(!DY._msgEl){
        DY._msgEl = $('<div class="dy-message"></div>').appendTo(document.body);
    }
    
    DY._msgEl.html(msg).show().css({marginTop:0, opacity:0}).animate({marginTop:-30, opacity:.9}, 300);
    
    clearTimeout(DY._msgTimer);
    DY._msgTimer = setTimeout(function(){
        DY._msgEl.fadeOut();
    }, 3000);
    
};
$.fn.dyMenu = function(){
    this.each(function(o){
        var menus = $(this).find('li');
        
        menus.each(function(i){
            $(this).click(function(e){
                menus.removeClass('on').eq(i).addClass('on');
                return false;
            });
        });
    });
};



var dyTabmenu = function(el, onClick){
    
    var menus = el.find('.dy-tab-menu li');
    var panels = el.children('.dy-tab-panel').hide();
    
    menus.each(function(i){
        $(this).click(function(e){
            panels.hide().removeClass('on').eq(i).show().addClass('on');
            menus.removeClass('on').eq(i).addClass('on');
            
            if(onClick){
                onClick(i, menus.eq(i), panels.eq(i));
            }
            
            return false;
        });
        
        if($(this).hasClass('on')){
            panels.eq(i).show().addClass('on');
        }
    });
    
    return {
        active: function(idx){
            panels.hide().removeClass('on').eq(idx).show().addClass('on');
            menus.removeClass('on').eq(idx).addClass('on');
        }
    };
};

var dySelectQuantity = function(el, onClick){
    
    var ipt = el.find('input');
    var btnAdd = el.find('a.add');
    var btnRemove = el.find('a.remove');
    
    ipt.keyup(function(e){
        DY.checkNumber(ipt);
        if(onClick){onClick(ipt.val());}
    });
    btnAdd.click(function(e){
        var n = parseInt(ipt.val());
        ipt.val(n+1);
        DY.checkNumber(ipt);
        if(onClick){onClick(ipt.val());}
        return false;
    });
    btnRemove.click(function(e){
        var n = parseInt(ipt.val());
        ipt.val(n-1);
        DY.checkNumber(ipt);
        if(onClick){onClick(ipt.val());}
        return false;
    });
    
};


document.oncontextmenu = function() {return false;};
$(document).mousedown(function(e){ 
    if( e.button == 2 ) { 
        return false; 
    } 
    return true; 
}); 